package dvdstore.server;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Set;

import common.dvd.DVD;
import common.rmi.DVDStoreServerInterface;

import dvdstore.database.DBAccessor;

public class DVDStoreServer implements DVDStoreServerInterface{

	private DBAccessor dba; 
	
	private DVDStoreServer() {
		dba = new DBAccessor("database.txt");
	}

	@Override
	public Set<DVD> getAllDVDs() throws RemoteException {
		return dba.readAllRecords();
	}
	
	@Override
	public DVD getDVDByID(Long id) throws RemoteException {
		
		return dba.readRecord(id);
	}

	public static void main(String[] args) {
		
		System.setProperty("java.security.policy","C:\\dvdserver.policy");
		
		if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
		
		try {
            String name = "DVDStoreServer";
            DVDStoreServerInterface dvdStoreServer = new DVDStoreServer();
            DVDStoreServerInterface stub =
                (DVDStoreServerInterface) UnicastRemoteObject.exportObject(dvdStoreServer, 0);
            Registry registry = LocateRegistry.createRegistry(1099);
            registry.rebind(name, stub);
            System.out.println("DVDStoreServer bound");
        } catch (Exception e) {
            System.err.println("DVDStoreServer exception:");
            e.printStackTrace();
        }
	}

	
}
